package apis

import (
	"fmt"
	"github.com/gin-gonic/gin"
	dto "vrcm/src/vrcm/app/admin/service/dto/subject"
	service "vrcm/src/vrcm/app/admin/service/subject"
	"vrcm/src/vrcm/common/apis"
)

type Statistics struct {
	apis.Api
}

// GetStatisticsBySubject  该题目答对人数/答错人数
func (e Statistics) GetStatisticsBySubject(c *gin.Context) {
	req := dto.StatisticsFilterReq{}
	s := service.Statistics{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	data, err := s.GetStatisticsBySubject(&req)
	if err != nil {
		e.Error(200, err, fmt.Sprintf("获取AnswerRecord失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.OK(data, "查询成功")
}

// GetPerSubjectStatisticsByFilter  该题目答对人数/答错人数
func (e Statistics) GetPerSubjectStatisticsByFilter(c *gin.Context) {
	req := dto.StatisticsBySubjectReq{}
	s := service.Statistics{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	data, err := s.GetPerSubjectStatisticsByFilter(&req)
	if err != nil {
		e.Error(200, err, fmt.Sprintf("获取AnswerRecord失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.OK(data, "查询成功")
}

// GetStatisticsByAccount  根据条件统计用户答题结果
func (e Statistics) GetStatisticsByAccount(c *gin.Context) {
	req := dto.StatisticsFilterReq{}
	s := service.Statistics{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	data, err := s.GetStatisticsByAccount(&req)
	if err != nil {
		e.Error(200, err, fmt.Sprintf("获取AnswerRecord失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.OK(data, "查询成功")
}

// GetPerStatisticsByAccount  根据条件统计用户答题结果
func (e Statistics) GetPerStatisticsByAccount(c *gin.Context) {
	req := dto.StatisticsByAccountReq{}
	s := service.Statistics{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	data, err := s.GetPerStatisticsByAccount(&req)
	if err != nil {
		e.Error(200, err, fmt.Sprintf("获取AnswerRecord失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.OK(data, "查询成功")
}

// GetVoteStatistics  投票参与人数统计
func (e Statistics) GetVoteStatistics(c *gin.Context) {
	req := dto.VoteStatisticsReq{}
	s := service.Statistics{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	data, err := s.GetVoteStatistics(&req)
	if err != nil {
		e.Error(apis.VoteStatisticsError.Code, err, fmt.Sprintf("GetVoteStatistics失败，\r\n失败信息 %s", err.Error()))
		return
	}
	e.OK(data, "查询成功")
}

// GetVoteStatisticsDetail  投票参与统计详情
func (e Statistics) GetVoteStatisticsDetail(c *gin.Context) {
	req := dto.VoteStatisticsDetailReq{}
	s := service.Statistics{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	data, err := s.GetVoteStatisticsDetail(&req)
	if err != nil {
		e.Error(apis.VoteStatisticsError.Code, err, fmt.Sprintf("GetVoteStatistics失败，\r\n失败信息 %s", err.Error()))
		return
	}
	e.OK(data, "查询成功")
}

// GetQuestionStatistics  获取问卷统计
func (e Statistics) GetQuestionStatistics(c *gin.Context) {
	req := dto.QuestionStatisticsReq{}
	s := service.Statistics{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	data, err := s.GetQuestionStatistics(&req)
	if err != nil {
		e.Error(apis.VoteStatisticsError.Code, err, fmt.Sprintf("GetQuestionStatistics失败，\r\n失败信息 %s", err.Error()))
		return
	}
	e.OK(data, "查询成功")
}

// GetQuestionStatisticsDetail  获取问卷统计详情
func (e Statistics) GetQuestionStatisticsDetail(c *gin.Context) {
	req := dto.QuestionStatisticsDetailReq{}
	s := service.Statistics{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	data, err := s.GetQuestionStatisticsDetail(&req)
	if err != nil {
		e.Error(apis.VoteStatisticsError.Code, err, fmt.Sprintf("GetQuestionStatisticsDetail失败，\r\n失败信息 %s", err.Error()))
		return
	}
	e.OK(data, "查询成功")
}

// GetSignInStatistics  获取签到统计
func (e Statistics) GetSignInStatistics(c *gin.Context) {
	req := dto.SignInStatisticsReq{}
	s := service.Statistics{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	data, err := s.GetSignInStatistics(&req)
	if err != nil {
		e.Error(apis.VoteStatisticsError.Code, err, fmt.Sprintf("GetSignInStatistics失败，\r\n失败信息 %s", err.Error()))
		return
	}
	e.OK(data, "查询成功")
}

// GetSignInStatisticsDetail  获取签到统计详情
func (e Statistics) GetSignInStatisticsDetail(c *gin.Context) {
	req := dto.SignInStatisticsDetailReq{}
	s := service.Statistics{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	data, err := s.GetSignInStatisticsDetail(&req)
	if err != nil {
		e.Error(apis.VoteStatisticsError.Code, err, fmt.Sprintf("GetSignInStatisticsDetail失败，\r\n失败信息 %s", err.Error()))
		return
	}
	e.OK(data, "查询成功")
}

// GetLikesStatistics  获取点赞统计
func (e Statistics) GetLikesStatistics(c *gin.Context) {
	req := dto.LikesStatisticsReq{}
	s := service.Statistics{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	e.Logger.Error("YYYYYYYYYYYYYYYYYY:", req)
	data, err := s.GetLikeStatistics(&req)
	if err != nil {
		e.Error(apis.LikesStatisticsError.Code, err, fmt.Sprintf("GetSLikesStatistics失败，\r\n失败信息 %s", err.Error()))
		return
	}
	e.OK(data, "查询成功")
}

// GetLikesStatisticsDetail  获取点赞统计详情
func (e Statistics) GetLikesStatisticsDetail(c *gin.Context) {
	req := dto.LikesStatisticsDetailReq{}
	s := service.Statistics{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	data, err := s.GetLikeStatisticsDetail(&req)
	if err != nil {
		e.Error(apis.LikesStatisticsError.Code, err, fmt.Sprintf("GetSLikesStatistics失败，\r\n失败信息 %s", err.Error()))
		return
	}
	e.OK(data, "查询成功")
}
